Preserve meaningful last_error in LibevConnection close paths#700
Open
dkropachev wants to merge 1 commit intomasterfrom
Open
Preserve meaningful last_error in LibevConnection close paths#700dkropachev wants to merge 1 commit intomasterfrom
dkropachev wants to merge 1 commit intomasterfrom
Conversation
8543c1f to
6aabcee
Compare
LibevConnection.close() closes the socket immediately while watchers are stopped asynchronously in the next event loop iteration via _loop_will_run(). This creates a race window where handle_read() or handle_write() can operate on a closed socket fd, producing EBADF errors that surface as ConnectionShutdown. - Add is_closed/is_defunct guards in handle_read() and handle_write() error paths to silently exit during shutdown instead of calling defunct() with EBADF - Set last_error in close() when connected_event is not yet set to prevent factory() from returning a dead connection - Set last_error on server-initiated close (EOF) in handle_read() before calling close()
6aabcee to
96add52
Compare
Lorak-mmk
reviewed
Feb 16, 2026
Comment on lines
+384
to
386
| self.last_error = ConnectionShutdown( | ||
| "Connection to %s was closed by server" % self.endpoint) | ||
| self.close() |
There was a problem hiding this comment.
Why do you set it unconditionally here, but guard it with if not self.connected_event.is_set(): in the other place?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Improves error reporting in LibevConnection so that users see a clear
ConnectionShutdownmessage instead of a confusing[Errno 9] Bad file descriptorwhen connections close during node restarts.The libev reactor was already safe from the EBADF race —
defunct()returns early whenis_closedis true, and the single-threaded event loop +_loop_will_run()design prevents watchers from firing on closed fds. The actual problem was thatlast_errorwasn't set to a meaningful value in two close paths, so the stale EBADF string leaked up tofactory()and confused users.Changes
last_errorinclose()whenconnected_eventis not yet set, sofactory()reportsConnectionShutdowninstead of whatever stale error was on the connectionlast_erroron server-initiated close (EOF) inhandle_read()before callingclose()Test plan
tests/unit/io/test_libevreactor.pypassesRefs #614